<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    	<title>统战成员学习平台</title>
	    <link rel="stylesheet" type="text/css" href="../../css/base.css" />
	    <link rel="stylesheet" type="text/css" href="../../css/style.css" />
	</head>
	<body class="white">
		<div id='app' style="display:none" v-show="true">
			<div class="question_no tac">
				<div>
					<i class="icon_hourglass"></i>剩余时间：<span id="timeText">00:00</span>
				</div>
			</div>
			<ul class="question_list" :style="{'height': height - 155 + 'px'}">
				<li v-for="(item, i) of questions"  @click="selectQuestion(i, item)" :class="{'active': item.answer.length}"><span>{{i+1}}</span></li>
			</ul>
			<div class="question_bottom">
				<i class="icon_question_list"></i><strong>{{answered}}</strong>/{{questions.length}}
				<span tapmode="tap" @click="submit()" class="btn_yellow">交卷</span>
			</div>
		</div>
		<script type="text/javascript" src="../../script/api.js"></script>
		<script type="text/javascript" src="../../script/vue.min.js"></script>
		<script type="text/javascript" src="../../script/common.js"></script>
		<script type="text/javascript" src="../../script/layer_mobile/layer.js"></script>
		<script type="text/javascript">
		    new Vue({
				el: '#app',
		     	data: {
					scheduleId: 0,
					height: 400,
					questions: [],
					answered: 0,
					examTime: 0,
					stratTime: 0,
					courseId: 0,
					leaningGroupsId: 0,
					type: 0,
					name: ''
		     	},
				mounted: function() {
		        	var $this = this;
		        	apiready = function() {
						$this.height = api.frameHeight;
						$this.examTime = api.pageParam.examTime;
						$this.stratTime = api.pageParam.stratTime;
						$this.courseId = api.pageParam.courseId;
						$this.leaningGroupsId = api.pageParam.leaningGroupsId;
						$this.type = api.pageParam.type;
						$this.name = api.pageParam.name;
						$this.scheduleId = api.pageParam.scheduleId;
		                var now = Date.now(),
		                	lastTime  = $this.examTime * 60 * 1000 - (now - $this.stratTime),
		                    m, s;
		                function timer() {
		                	lastTime -= 1000;
		                    if(lastTime > 0) {
		                        m = Math.floor(lastTime / 1000 / 60);
		                        s = Math.floor((lastTime - m * 1000 * 60) / 1000);
		                        timeText.innerText = addo(m) + ':' + addo(s);
		                    } else {
		                        timeText.innerText = "00:00";
		                        clearInterval(flag);
		                    }
						}
						timer();
		                var flag = setInterval(function() {
		                    timer()
		                }, 1000);
						$this.init();
						parseTapmode();
                	}
		    	},
		    	methods: {
					init: function() {
			        	var $this = this;
						$this.questions = $api.getStorage(_.EXAMINGDATA);
						$this.questions.forEach(function(item) {
							if(item.answer.length > 0) {
								$this.answered ++;
							}
						})
					},
					selectQuestion: function(i, item) {
						if(!item.answer.length) return;
						api.sendEvent({
						    name: 'switch_question_index_event',
						    extra: {
						        index: i
						    }
						});
						closeWin();
					},
					submit: function() {
			        	var $this = this;
						api.confirm({
		                    title: '提示',
		                    msg: '确认交卷吗？',
		                    buttons: ['确定', '取消']
		                }, function(ret, err){
		                    if(ret.buttonIndex === 1) {
								$this.submitPaper()
		                    }
		                });
					},
					submitPaper: function() {
			        	var $this = this;
						var answerList = [], answer;
						$this.questions.forEach(function(item) {
							answer = item.answer;
							if(Object.prototype.toString.call(answer) === '[object Array]') {
								answer = answer.join(',')
							}
							answerList.push({
								qustionId: item.qustionId,
								answer: answer
							})
						});
						api.showProgress({
							title: '交卷中...',
							text: ''
						});
						var examData = $api.getStorage(_.EXAMLIST);
						post('/client/exam/submitPaper', {
							courseId: $this.courseId,
							scoreId: examData.examId,
							paperMangerId: examData.paperMangerId,
							leaningGroupsId: $this.leaningGroupsId,
							type: $this.type,
							answerList: answerList
						}, function(ret) {
							if(ret.isSuccess) {
								api.sendEvent({
									name: 'refresh_learn_center_event'
								});
								$api.setStorage(_.EXAMRESULT, ret.data);
								openWin('exam_result_win', {name: $this.name, type: $this.type, scheduleId: $this.scheduleId})
							} else {
								msgUtil.toast({
									msg: ret.message
								});
							}
						})
					}
		    	}
		    });
		</script>
	</body>
</html>
